package com.zw.re.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;

/**
 * Created by zwz
 * date 2016/11/29
 * project com.zw.re.service
 */
@Service
public class HashSetOpt {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SerializerUtil serializerUtil;

    @Resource(name = "jackson2JsonRedisSerializer")
    private RedisSerializer jackson2JsonRedisSerializer;

    /**
     * 插入一个set集合
     *
     * @param key
     * @param object
     * @return
     */
    public Long insertAll(Object key, Set object) {
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        final byte[] rawKey = serializerUtil.serializerKey(key);
        final byte[][] rawValues = serializerUtil.serializerValue(object);
        return (Long) redisTemplate.execute(new RedisCallback<Long>() {

            public Long doInRedis(RedisConnection connection) {
                return connection.sAdd(rawKey, rawValues);
            }
        }, true);
    }

    /**
     * 插入一组数据
     *
     * @param key
     * @return
     */
    public Long insert(Object key, Object objects) {
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        return redisTemplate.opsForSet().add(key, objects);
    }

    /**
     * 查询所有的
     *
     * @param key
     * @return
     */
    public Set queryAll(Object key) {
        redisTemplate.setValueSerializer(redisTemplate.getDefaultSerializer());
        return redisTemplate.opsForSet().members(key);
    }


    /**
     * 判断是否存在 一个值
     *
     * @param key
     * @param value
     * @return
     */
    public boolean exist(Object key, Object value) {
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        return redisTemplate.opsForSet().isMember(key, value);
    }

    /**
     * 获取set长度
     *
     * @param key
     * @return
     */
    public Long size(Object key) {
        return redisTemplate.opsForSet().size(key);
    }

    /**
     * 删除一组数据
     *
     * @param key
     * @param objects Object...
     */
    public Long delete(Object key, Object objects) {
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        return redisTemplate.opsForSet().remove(key, objects);
    }

    /**
     * 根据index 删除
     *
     * @param key
     * @param set Set
     */
    public Long deleteSet(Object key, Set set) {
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        final byte[] rawKey = serializerUtil.serializerKey(key);
        final byte[][] rawValues = serializerUtil.serializerValue(set);
        return (Long) redisTemplate.execute(new RedisCallback<Long>() {

            public Long doInRedis(RedisConnection connection) {
                return connection.sRem(rawKey, rawValues);
            }
        }, true);
    }
}
